package com.db.compare.controller;

import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseController;
import com.db.compare.core.BaseResponse;
import com.db.compare.mapper.RbacMenuMapper;
import com.db.compare.model.RbacMenu;
import com.db.compare.service.RbacMenuService;
import com.db.compare.vo.RbacMenuVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;


/**
* 菜单管理
* */
@Slf4j
@Controller
@RequestMapping("/rbac/menu")
public class RbacMenuController extends BaseController{

    @Autowired
    private RbacMenuMapper rbacmenuMapper;
    @Autowired
    private RbacMenuService rbacmenuService;

    @RequestMapping(value={"/list",""})
    public String rbacmenuList(){
        return "/html/RbacMenu";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<RbacMenu> pages(Model model,@RequestParam Map rbacmenuMap){
        RbacMenuVo rbacmenuVo = JSON.parseObject(JSON.toJSONString(rbacmenuMap), RbacMenuVo.class);
        return rbacmenuService.pageInfo(rbacmenuVo);
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        rbacmenuMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody RbacMenu rbacmenu){
        rbacmenu.setUpdateTime(new Date());
        if(rbacmenu.getId()==null) {
            rbacmenu.setCreateTime(new Date());
            rbacmenuMapper.insert(rbacmenu);
        }else{
            rbacmenuMapper.updateByPrimaryKey(rbacmenu);
        }
        return new BaseResponse(200,rbacmenu);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        RbacMenu rbacmenu = rbacmenuMapper.selectByPrimaryKey(pk);
        return new BaseResponse(200,rbacmenu);
    }
    //下载模板
    @RequestMapping("/download")
    public void testRespExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String ids=request.getParameter("ids");
        rbacmenuService.download(ids,response);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public BaseResponse upload(@RequestParam("file") MultipartFile file){
        if(!rbacmenuService.upload(file)){
            return buildFailResponse();
        }

        return buildSuccessResponse();
    }
}